<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <title>砺锋智慧健康系统</title>
    <meta content="砺锋智慧健康系统" name="description">
    <meta content="砺锋智慧健康系统" name="keywords">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"
          name="viewport">
    <!-- 引入样式 -->
    <link href="../static/plugins/elementui/index.css" rel="stylesheet">
    <link href="../static/plugins/font-awesome/css/font-awesome.css" rel="stylesheet">
    <link href="../static/css/style.css" rel="stylesheet">
    <!-- 引入组件库 -->
    <script src="../static/js/jquery.min.js" type="text/javascript"></script>
    <script src="../static/js/vue.js"></script>
    <script src="../static/plugins/elementui/index.js"></script>
    <script src="../static/js/axios-0.27.2.js"></script>
    <style>
      .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }

      .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
      }

      .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
      }

      .avatar {
        width: 178px;
        height: 178px;
        display: block;
      }

      .datatable {
        position: relative;
        box-sizing: border-box;
        -webkit-box-flex: 1;
        width: 100%;
        max-width: 100%;
        font-size: 14px;
        color: rgb(96, 98, 102);
        overflow: hidden;
        flex: 1 1 0%;
      }

      .datatable td, .datatable th {
        padding: 12px 0;
        min-width: 0;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        text-overflow: ellipsis;
        vertical-align: middle;
        position: relative;
        text-align: left;
      }
    </style>
  </head>
  <body class="hold-transition">
    <div id="app">
      <div class="content-header">
        <h1>预约管理<small>套餐管理</small></h1>
        <el-breadcrumb class="breadcrumb" separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>预约管理</el-breadcrumb-item>
          <el-breadcrumb-item>套餐管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="app-container">
        <div class="box">
          <div class="filter-container">
            <el-input @keyup.enter.native="handleFilter" class="filter-item"
                      placeholder="编码/名称/助记码"
                      style="width: 200px;" v-model="pagination.queryString"></el-input>
            <el-button @click="findPage2()" class="dalfBut">查询</el-button>
            <el-button @click="handleCreate()" class="butT" type="primary">新建</el-button>
          </div>
          <el-table :data="dataList" current-row-key="id" highlight-current-row size="small" stripe>
            <el-table-column align="center" label="序号" type="index"></el-table-column>
            <el-table-column align="center" label="套餐编码" prop="code"></el-table-column>
            <el-table-column align="center" label="套餐名称" prop="name"></el-table-column>
            <el-table-column align="center" label="适用性别">
              <template slot-scope="scope">
                <span>{{ scope.row.sex == '0' ? '不限' : scope.row.sex == '1' ? '男' : '女'}}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="适用年龄" prop="age"></el-table-column>
            <el-table-column align="center" label="助记码" prop="helpCode"></el-table-column>
            <el-table-column align="center" label="说明" prop="remark"></el-table-column>
            <el-table-column align="center" label="操作">
              <template slot-scope="scope">
                <el-button @click="handleUpdate(scope.row)" size="mini" type="primary">编辑
                </el-button>
                <el-button @click="handleDelete(scope.row)" size="mini" type="danger">删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
                :total="pagination.total"
                @current-change="handleCurrentChange"
                :current-page="pagination.currentPage"
                :page-size="pagination.pageSize"
                class="pagiantion"
                layout="total, prev, pager, next, jumper">
            </el-pagination>
          </div>
          <!-- 新增标签弹层 -->
          <div class="add-form">
            <el-dialog :visible.sync="dialogFormVisible" title="新增套餐">
              <template>
                <el-tabs type="card" v-model="activeName">
                  <el-tab-pane label="基本信息" name="first">
                    <el-form label-position="right" label-width="100px">
                      <el-row>
                        <el-col :span="12">
                          <el-form-item label="编码">
                            <el-input v-model="formData.code"/>
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="名称">
                            <el-input v-model="formData.name"/>
                          </el-form-item>
                        </el-col>
                      </el-row>
                      <el-row>
                        <el-col :span="12">
                          <el-form-item label="适用性别">
                            <el-select v-model="formData.sex">
                              <el-option label="不限" value="0"></el-option>
                              <el-option label="男" value="1"></el-option>
                              <el-option label="女" value="2"></el-option>
                            </el-select>
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="助记码">
                            <el-input v-model="formData.helpCode"/>
                          </el-form-item>
                        </el-col>
                      </el-row>
                      <el-row>
                        <el-col :span="12">
                          <el-form-item label="套餐价格">
                            <el-input v-model="formData.price"/>
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="适用年龄">
                            <el-input v-model="formData.age"/>
                          </el-form-item>
                        </el-col>
                      </el-row>
                      <el-row>
                        <el-col :span="24">
                          <el-form-item label="上传图片">
                            <!-- element-ui 的上传组件-->
                            <!--
                                el-upload：上传组件
                                action：上传的提交地址
                                auto-upload：选中⽂件后是否⾃动上传
                                name：上传⽂件的名称，服务端可以根据名称获得上传的⽂件对象
                                show-file-list：是否显示已上传⽂件列表
                                on-success：⽂件上传成功时的钩⼦
                                before-upload：上传⽂件之前的钩⼦
                             -->
                            <el-upload
                                :auto-upload="autoUpload"
                                action="/setmeal/upload"
                                :before-upload="beforeAvatarUpload"
                                :on-success="handleAvatarSuccess"
                                :show-file-list="false"
                                class="avatar-uploader"
                                name="imgFile">
                              <!--用于上传图片的预览-->
                              <img :src="imageUrl" class="avatar" v-if="imageUrl">
                              <!--用于展示上传图标-->
                              <i class="el-icon-plus avatar-uploader-icon" v-else></i>
                            </el-upload>
                          </el-form-item>
                        </el-col>
                      </el-row>
                      <el-row>
                        <el-col :span="24">
                          <el-form-item label="说明">
                            <el-input type="textarea" v-model="formData.remark"></el-input>
                          </el-form-item>
                        </el-col>
                      </el-row>
                      <el-row>
                        <el-col :span="24">
                          <el-form-item label="注意事项">
                            <el-input type="textarea" v-model="formData.attention"></el-input>
                          </el-form-item>
                        </el-col>
                      </el-row>
                    </el-form>
                  </el-tab-pane>
                  <el-tab-pane label="检查组信息" name="second">
                    <div class="checkScrol">
                      <table class="datatable">
                        <thead>
                          <tr>
                            <th>选择</th>
                            <th>项目编码</th>
                            <th>项目名称</th>
                            <th>项目说明</th>
                          </tr>
                        </thead>
                        <tbody>
                          <tr v-for="c in tableData">
                            <td>
                              <input :id="c.id" :value="c.id" type="checkbox"
                                     v-model="checkgroupIds">
                            </td>
                            <td><label :for="c.id">{{c.code}}</label></td>
                            <td><label :for="c.id">{{c.name}}</label></td>
                            <td><label :for="c.id">{{c.remark}}</label></td>
                          </tr>
                        </tbody>
                      </table>
                    </div>
                  </el-tab-pane>
                </el-tabs>
              </template>
              <div class="dialog-footer" slot="footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button @click="handleAdd()" type="primary">确定</el-button>
              </div>
            </el-dialog>
          </div>

          <!-- 编辑标签弹层 -->
          <div class="add-form">
            <el-dialog :visible.sync="dialogFormVisibleEdit" title="编辑套餐">
              <template>
                <el-tabs type="card" v-model="activeName">
                  <el-tab-pane label="基本信息" name="first">
                    <el-form label-position="right" label-width="100px">
                      <el-row>
                        <el-col :span="12">
                          <el-form-item label="编码">
                            <el-input v-model="formData.code"/>
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="名称">
                            <el-input v-model="formData.name"/>
                          </el-form-item>
                        </el-col>
                      </el-row>
                      <el-row>
                        <el-col :span="12">
                          <el-form-item label="适用性别">
                            <el-select v-model="formData.sex">
                              <el-option label="不限" value="0"></el-option>
                              <el-option label="男" value="1"></el-option>
                              <el-option label="女" value="2"></el-option>
                            </el-select>
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="助记码">
                            <el-input v-model="formData.helpCode"/>
                          </el-form-item>
                        </el-col>
                      </el-row>
                      <el-row>
                        <el-col :span="12">
                          <el-form-item label="套餐价格">
                            <el-input v-model="formData.price"/>
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="适用年龄">
                            <el-input v-model="formData.age"/>
                          </el-form-item>
                        </el-col>
                      </el-row>
                      <el-row>
                        <el-col :span="24">
                          <el-form-item label="上传图片">
                            <el-upload
                                class="avatar-uploader"
                                :before-upload="beforeAvatarUpload"
                                name="imgFile"
                                :show-file-list="false"
                                :on-success="handleAvatarSuccess"
                                action="/setmeal/upload"
                            >
                              <!-- 用于上传图片的预览，v-if的条件为真才渲染，v-show一直渲染 -->
                              <img :src="imageUrl" class="avatar" v-if="imageUrl">
                              <!--用于上传图标-->
                              <i class="el-icon-plus avatar-uploader-icon" v-else></i>
                            </el-upload>
                          </el-form-item>
                        </el-col>
                      </el-row>
                      <el-row :span="24">
                        <el-form-item label="说明">
                          <el-input type="textarea" v-model="formData.remark"></el-input>
                        </el-form-item>
                      </el-row>
                      <el-row>
                        <el-col :span="24">
                          <el-form-item label="注意事项">
                            <el-input type="textarea" v-model="formData.attention"></el-input>
                          </el-form-item>
                        </el-col>
                      </el-row>
                    </el-form>
                  </el-tab-pane>
                  <el-tab-pane label="检查组信息" name="second">
                    <div class="checkScrol">
                      <table class="datatable">
                        <thead>
                          <tr>
                            <th>选择</th>
                            <th>项目编码</th>
                            <th>项目名称</th>
                            <th>项目说明</th>
                          </tr>
                        </thead>
                        <tbody>
                          <tr v-for="c in tableData">
                            <td>
                              <input :id="c.id" :value="c.id" type="checkbox"
                                     v-model="checkgroupIds">
                            </td>
                            <td><label :for="c.id">{{c.code}}</label></td>
                            <td><label :for="c.id">{{c.name}}</label></td>
                            <td><label :for="c.id">{{c.remark}}</label></td>
                          </tr>
                        </tbody>
                      </table>
                    </div>
                  </el-tab-pane>
                </el-tabs>
              </template>
              <div class="dialog-footer" slot="footer">
                <el-button @click="dialogFormVisibleEdit = false">取消</el-button>
                <el-button @click="handleEdit()" type="primary">确定</el-button>
              </div>
            </el-dialog>
          </div>
        </div>
      </div>
    </div>
  </body>


  <script>
    var vue = new Vue({
      el: '#app',
      data: {
        // 自动上传
        autoUpload: true,
        // 模型数据，用于上传图片完成后图片预览
        imageUrl: null,
        // 添加编辑窗口Tab标签名称
        activeName: 'first',
        // 分页相关模型数据
        pagination: {
          // 当前页码
          currentPage: 1,
          // 每页显示的记录数
          pageSize: 10,
          // 总记录数
          total: 0,
          // 查询条件
          queryString: null
        },
        // 当前页要展示的分页列表数据
        dataList: [],
        // 表单数据
        formData: {},
        // 添加表单窗口中检查组列表数据
        tableData: [],
        // 添加表单窗口中检查组复选框对应id
        checkgroupIds: [],
        // 控制添加窗口显示/隐藏
        dialogFormVisible: false,
        //控制编辑窗口显示/隐藏
        dialogFormVisibleEdit: false
      },
      created() {
        this.findPage();
      },
      methods: {
        //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
        handleAvatarSuccess(response, file) {
          //为模型数据imageUrl赋值，用于页面图片预览

          this.imageUrl = 'http://rycbhriro.hn-bkt.clouddn.com/' + response.data;
          this.$message({
            type: response.flag ? 'success' : 'error',
            message: response.message
          });
          //设置模型数据（图片名称），后续提交ajax请求时会提交到后台最终保存到数据库
          this.formData.img = response.data;
        },
        //上传图片之前执行
        beforeAvatarUpload(file) {
          const isJPG = file.type === 'image/jpeg';
          const isLt2M = file.size / 1024 / 1024 < 2;
          if (!isJPG) {
            this.$message.error('上传套餐图片只能是 JPG 格式!');
          }
          if (!isLt2M) {
            this.$message.error('上传套餐图片大小不能超过 2MB!');
          }
          return isJPG && isLt2M;
        },
        //添加
        handleAdd() {
          //发送ajax请求，将表单数据（套餐基本信息、检查组ID）提交到后台进行处理
          axios.post("/setmeal/add?checkgroupIds=" + this.checkgroupIds, this.formData).then(
              (res) => {
                //关闭新增窗口
                this.dialogFormVisible = false;
                if (res.data.flag) {
                  //执行成功
                  this.$message({
                    type: 'success',
                    message: res.data.message
                  });
                } else {
                  //执行失败
                  this.$message.error(res.data.message);
                }
              }).finally(() => {
            this.findPage();
          });
        },
        //分页查询
        findPage() {
          //分页参数
          var param = {
            currentPage: this.pagination.currentPage,//页码
            pageSize: this.pagination.pageSize,//每页显示的记录数
            queryString: this.pagination.queryString//查询条件
          };
          //请求后台
          axios.post("/setmeal/findPage", param).then((response) => {
            //为模型数据赋值，基于VUE的双向绑定展示到页面
            this.dataList = response.data.rows;
            this.pagination.total = response.data.total;
          });
        },

        findPage2() {
          this.pagination.currentPage = 1;
          this.findPage();
        },
        // 重置表单
        resetForm() {
          this.formData = {};
          this.activeName = 'first';
          this.imageUrl = null;
          this.checkgroupIds = [];
        },
        // 弹出添加窗口
        handleCreate() {
          this.resetForm();
          this.dialogFormVisible = true;
          //发送ajax请，查询所有的检查组数据，转为json展示到当前新增窗口中
          axios.get("/checkgroup/findAll").then((res) => {
            if (res.data.flag) {
              //查询成功
              this.tableData = res.data.data;
            } else {
              //查询失败
              this.$message.error(res.data.message);
            }
          });
        },
        //切换页码
        handleCurrentChange(currentPage) {
          this.pagination.currentPage = currentPage;
          this.findPage();
        },

        // 弹出编辑窗口（meal基础信息、所有检查组信息，选中的检查组）
        handleUpdate(row) {
          this.dialogFormVisibleEdit = true;
          this.activeName = 'first';
          this.resetForm();

          axios.get("/setmeal/findMealById?id=" + row.id).then((res) => {
            if (res.data.flag) {
              this.formData = res.data.data;
              this.imageUrl = 'http://rycbhriro.hn-bkt.clouddn.com/' + res.data.data.img;
            } else {
              this.$message.error(res.data.message);
            }
          });

          axios.get("/checkgroup/findAll").then((res) => {
            if (res.data.flag) {
              this.tableData = res.data.data;
              axios.get("/setmeal/findCheckgroupIdsByMealId?id=" + row.id).then((res) => {
                if (res.data.flag) {
                  this.checkgroupIds = res.data.data;
                } else {
                  this.$message.error(res.data.message);
                }
              });
            } else {
              this.$message.error(res.data.message);
            }
          });
        },
        // 编辑
        handleEdit() {
          axios.post("/setmeal/edit?checkgroupIds=" + this.checkgroupIds, this.formData).then(
              (res) => {
                this.dialogFormVisibleEdit = false;
                if (res.data.flag) {
                  this.$message.success(res.data.message);
                } else {
                  this.$message.error(res.data.message);
                }
              }).finally(() => {
            this.findPage();
          });
        },
        // 删除
        handleDelete(row) {
          axios.get("/setmeal/delete?id=" + row.id).then((res) => {
            if (res.data.flag) {
              this.$message.success(res.data.message);
            } else {
              this.$message.error(res.data.message);
            }
          }).finally(() => {
            this.findPage();
          });
        }
      }
    })
  </script>
</html>
